class Shop::SeckillSpusController < ShopController
  def index
    @seckill_spus = SeckillSpu.joins(:spu).where("spus.shop_id = ?", current_shoper.shop.id).order("created_at DESC").page(params[:page]).per(params[:per])
  end
  
  def show
    @seckill_spu = SeckillSpu.find params[:id]
  end

  def new
    @spus = current_shoper.shop.spus.order("created_at DESC")
    @seckill_spu = SeckillSpu.new
  end

  def create
    @seckill_spu = SeckillSpu.new seckill_spu_params
    @seckill_spu.state = :pending
    if @seckill_spu.save
      redirect_to shop_seckill_spus_path, notice: "保存成功！"
    else
      redirect_to shop_seckill_spus_path, notice: "保存失败！"
    end
  end

  def edit
    @spus = current_shoper.shop.spus.order("created_at DESC")
    @seckill_spu = SeckillSpu.find params[:id]
  end

  def update
    @seckill_spu = SeckillSpu.find params[:id]
    if @seckill_spu.update seckill_spu_params
      redirect_to shop_seckill_spus_path, notice: "更新成功！"
    else
      redirect_to shop_seckill_spus_path, notice: "更新失败！"
    end
  end
  
  def destroy
    @seckill_spu = SeckillSpu.find params[:id]
    if @seckill_spu.destroy
      redirect_to shop_seckill_spus_path, notice: "删除成功."
    else
      redirect_to shop_seckill_spus_path, notice: "删除失败."
    end
  end

  private

  def seckill_spu_params
    params.require(:seckill_spu).permit(:spu_id, :seckill_price, :start_at, :end_at, :amount)
  end
end
